<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script type="importmap">
      {
        "imports": {
          "jest-mock": "https://esm.sh/jest-mock",
          "react": "https://esm.sh/react@18.3.1",
          "react-dom": "https://esm.sh/react-dom@18.3.1",
          "react-dom/": "https://esm.sh/react-dom@18.3.1/"
        }
      }
    </script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script type="module">
      import React from 'react';
      window.React = React;
    </script>
    <script crossorigin="anonymous" defer src="/__dist__/webchat-es5.js"></script>
    <script crossorigin="anonymous" defer src="/__dist__/botframework-webchat-debug-theme.development.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="module">
      import { fn, spyOn } from 'jest-mock';
      import React, { createElement, memo } from 'react';
      import { createRoot } from 'react-dom/client';

      run(async function () {
        const {
          testHelpers: { createStore },
          WebChat: {
            createDirectLine,
            DebugProvider,
            hooks: { useActivities, useBuildRenderActivityCallback },
            middleware: { activityComponent, createActivityPolymiddleware },
            ReactWebChat
          }
        } = window;

        const onTelemetry = fn();
        const rootElement = document.getElementsByTagName('main')[0];

        // WHEN: Web Chat is being rendered with <DebugProvider>.
        createRoot(document.getElementById('webchat')).render(
          createElement(
            DebugProvider,
            {},
            createElement(ReactWebChat, {
              directLine: createDirectLine({ token: await testHelpers.token.fetchDirectLineToken() }),
              onTelemetry,
              store: createStore()
            })
          )
        );

        await pageConditions.uiConnected();

        const consoleWarn = fn(console.log.bind(console));

        spyOn(console, 'warn').mockImplementationOnce(consoleWarn);

        await pageObjects.sendMessageViaSendBox('unknown attachment');
        await pageConditions.numActivitiesShown(2);

        // THEN: Should have error log.
        //       Currently attachment middleware is using console.warn instead of console.error.
        //       Once we migrate to polymiddleware, it should be console.error.
        expect(consoleWarn).toHaveBeenCalledTimes(1);
        expect(consoleWarn.mock.calls[0][0]).toBe(
          'No renderer for attachment for screen reader of type "x-unknown-attachment"'
        );

        // THEN: Should emit as exception event.
        const exceptionEvent = onTelemetry.mock.calls.find(call => call[0]?.type === 'exception');

        expect(exceptionEvent).toBeTruthy();
        expect(exceptionEvent[0].error.message).toBe('No renderer for attachment of type "x-unknown-attachment"');

        await host.snapshot('local');
      });
    </script>
  </body>
</html>
